﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AdaptationProject
{
    public partial class CmdParams : Form
    {
        public CmdParams()
        {
            InitializeComponent();
        }

        private string oldValue;
        private string oldNameParam;

        public struct CmdInfo
        {
            public short order;
            public string paramName;
            public IList<string> values;

            public CmdInfo(short orderParam, string parName)
            {
                values = new List<string>();
                order = orderParam;
                paramName = parName;
            }

            public CmdInfo(short orderParam, string parName, string val)
            {
                values = new List<string>();

                order = orderParam;
                paramName = parName;
                values.Add(val);
            }
        }

        private IList<CmdInfo> cmdList = new List<CmdInfo>();

        private void tbAcceptableValues_TextChanged(object sender, EventArgs e)
        {

        }
        private void ClearNameParamTBoxes()
        {
            editParamName.Text = string.Empty;
        }
        private void ClearValueTBoxes()
        {
            editValue.Text = string.Empty;
        }

        private void ClearListBoxValues()
        {
            listBoxValueParam.Items.Clear();
        }
        private void ClearListBoxNames()
        {
            listBoxNameParam.Items.Clear();
        }

        void UpdateListValues(int index)
        {
            ClearListBoxValues();
            foreach (string value in cmdList[index].values)
            {
                listBoxValueParam.Items.Add(value);
            }
        }
        void UpdateListNames()
        {
            ClearListBoxNames();
            foreach (CmdInfo value in cmdList)
            {
                listBoxNameParam.Items.Add(value.paramName);
            }
        }

        private void butAddName_Click(object sender, EventArgs e)
        {
            CmdInfo cmdParam = new CmdInfo((byte)nubOrderNum.Value,
                                            editParamName.Text);
            cmdList.Add(cmdParam);
            listBoxNameParam.Items.Add(cmdParam.paramName);
            nubOrderNum.Value++;

            int oldSelectedItem = listBoxNameParam.SelectedIndex;
            ClearListBoxValues();

            if (listBoxNameParam.SelectedIndex == -1)
            {
                listBoxNameParam.SelectedIndex = oldSelectedItem == -1 ? 0 : oldSelectedItem;
            }

            ClearNameParamTBoxes();
        }

        private void listBoxNameParam_SelectedIndexChanged(object sender, EventArgs e)
        {
            ClearListBoxValues();
            int index = listBoxNameParam.SelectedIndex;
            
            editParamName.Text = cmdList[index].paramName;
            oldNameParam = editParamName.Text;
            UpdateListValues(index);
        }

        private void listBoxValueParam_SelectedIndexChanged(object sender, EventArgs e)
        {
            int indexValue = listBoxValueParam.SelectedIndex;
            int indexName = listBoxNameParam.SelectedIndex;
            editValue.Text = cmdList[indexName].values[indexValue];
            oldValue = editValue.Text;
        }

        private void btnAddValue_Click(object sender, EventArgs e)
        {
            int indexName = listBoxNameParam.SelectedIndex;
            if (indexName == -1)
            {
                MessageBox.Show("Имя параметра не выбрано", "Ошибка выбора имени параметра", MessageBoxButtons.OK);
                return;
            }
            cmdList[indexName].values.Add(editValue.Text);

            int oldSelectedItem = listBoxValueParam.SelectedIndex;
            UpdateListValues(indexName);

#if UPDATE_SELECTED_INDEX 
            if (listBoxValueParam.SelectedIndex == -1)
            {
                listBoxValueParam.SelectedIndex = oldSelectedItem == -1 ? 0 : oldSelectedItem;
            }
#endif
            ClearValueTBoxes();
        }

        private void butChangeValue_Click(object sender, EventArgs e)
        {
            int indexName = listBoxNameParam.SelectedIndex;
            if (indexName == -1)
            {
                MessageBox.Show("Имя параметра не выбрано", "Ошибка выбора имени параметра", MessageBoxButtons.OK);
                return;
            }

            int indexValue = cmdList[indexName].values.IndexOf(oldValue);
            if (indexValue == -1)
            {
                MessageBox.Show("Параметр не найден", "Ошибка поиск значения параметра", MessageBoxButtons.OK);
                return;
            }
            cmdList[indexName].values[indexValue] = editValue.Text;

            int oldSelectedItem = listBoxValueParam.SelectedIndex;
            UpdateListValues(indexName);
#if UPDATE_SELECTED_INDEX 
            if (listBoxValueParam.SelectedIndex == -1)
            {
                listBoxValueParam.SelectedIndex = oldSelectedItem == -1 ? 0 : oldSelectedItem;
            }
#endif
            ClearValueTBoxes();
        }

        private int FindIf(string value)
        {
            int i = 0;
            foreach (CmdInfo cmdParam in cmdList)
            {
                if (cmdParam.paramName == oldNameParam)
                {
                    return i;
                }
                ++i;
            }

            return -1;
        }

        private void butChangeName_Click(object sender, EventArgs e)
        {
            int indexName = FindIf(oldNameParam);
            if (indexName == -1)
            {
                MessageBox.Show("Имя параметра не найдено", "Ошибка поиск имение параметра", MessageBoxButtons.OK);
                return;
            }
            CmdInfo tmpInfo = cmdList[indexName];
            tmpInfo.paramName = editParamName.Text;
            cmdList[indexName] = tmpInfo;

            int oldSelectedItem = listBoxNameParam.SelectedIndex;
            
            UpdateListNames();
            UpdateListValues(oldSelectedItem);
#if UPDATE_SELECTED_INDEX 
            if (listBoxNameParam.SelectedIndex == -1)
            {
                listBoxNameParam.SelectedIndex = oldSelectedItem == -1 ? 0 : oldSelectedItem;
            }
#endif
        }

        private void butDeleteName_Click(object sender, EventArgs e)
        {
            int indexName = listBoxNameParam.SelectedIndex;
            if (indexName == -1)
            {
                MessageBox.Show("Значение не выбрано", "Ошибка выбора имя параметра", MessageBoxButtons.OK);
                return;
            }
            string questingText = string.Format("Вы действительно хотите удалить параметр: {0}, со всеми его значениями?",
                                                listBoxNameParam.SelectedItem);

            if (MessageBox.Show(questingText, "Предупреждение", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.No)
            {
                return;
            }
            cmdList.RemoveAt(indexName);
            UpdateListNames();
            ClearListBoxValues();
            if (listBoxNameParam.Items.Count > indexName)
            {
                listBoxNameParam.SelectedIndex = indexName;
            }
            else
            {
                listBoxNameParam.SelectedIndex = listBoxNameParam.Items.Count - 1;
            }
        }

        private void butDeleteValue_Click(object sender, EventArgs e)
        {
            int indexName = listBoxNameParam.SelectedIndex;
            if (indexName == -1)
            {
                MessageBox.Show("Имя параметра не выбрано", "Ошибка выбора имени параметра", MessageBoxButtons.OK);
                return;
            }

            int indexValue = listBoxValueParam.SelectedIndex;
            if (indexValue == -1)
            {
                MessageBox.Show("Параметр не найден", "Ошибка поиск значения параметра", MessageBoxButtons.OK);
                return;
            }
            string questingText = string.Format("Вы действительно хотите удалить значние: {0}, из параметра {1}?", 
                                                listBoxValueParam.SelectedItem, 
                                                listBoxNameParam.SelectedItem);

            if (MessageBox.Show(questingText, "Предупреждение", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.No)
            {
                return;
            }

            cmdList[indexName].values.RemoveAt(indexValue);
            UpdateListValues(indexName);

            if (listBoxValueParam.Items.Count > indexValue)
            {
                listBoxValueParam.SelectedIndex = indexValue;
            }
            else
            {
                listBoxValueParam.SelectedIndex = listBoxValueParam.Items.Count;
            }
        }

        /*


        private void btnClear_Click(object sender, EventArgs e)
        {
            nudOrderNum.Value = 0;
            localLst.Clear();
            lbAddedParameters.Items.Clear();
            ClearParamTBoxes();
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string fileName = openFileDialog1.SafeFileName;
                string filePath = openFileDialog1.FileName;
                if (fileName.EndsWith(DllExt))
                {
                    tbDllPath.Text = filePath;
                }
                else
                {
                    MessageBox.Show(Resources.ADDALGDescr_InvalidLibraryExtText, Resources.ADDALGDescr_InvalidLibraryExtTitle,
                                        MessageBoxButtons.OK);
                }
            }
        }

        private void btnFinish_Click(object sender, EventArgs e)
        {
            //VALIDATION OF THIS CODE!
            XMLParser localParset = new XMLParser();
            string path = Path.Combine(Settings.Default.AlgorithmsCachePath, tbFileName.Text);
            localParset.CreateTemplateDocument(path);
            localParset.Load(path);
            localParset.SetHeader(tbAlgName.Text, tbDllPath.Text, tbExePath.Text);
            foreach (ParameterInfo info in localLst)
            {
                localParset.AddParameter(info.order, info.paramName, info.description, info.values, info.defaultValue);
            }
            localParset.Save();
            localLst.Clear();
            ClearAll();
            Hide();
        }

        private void AddNewAlgorithm_Load(object sender, EventArgs e)
        {
            lbAddedParameters.SelectedIndexChanged += SelectedIndexChangedEventHandler;
        }

        private void SelectedIndexChangedEventHandler(object sender, EventArgs e)
        {
            
        }

        private void ClearParamTBoxes()
        {
            tbParamName.Text = string.Empty;
            tbDescription.Text = string.Empty;
            tbAcceptableValues.Text = string.Empty;
        }

        private void ClearAll()
        {
            ClearParamTBoxes();
            tbFileName.Text = string.Empty;
            tbDllPath.Text = string.Empty;
            tbAlgName.Text = string.Empty;
            tbExePath.Text = string.Empty;
            lbAddedParameters.Items.Clear();
        }

        private void btnExePath_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string fileName = openFileDialog1.SafeFileName;
                string filePath = openFileDialog1.FileName;
                if (fileName.EndsWith(ExeExt))
                {
                    tbExePath.Text = filePath;
                }
                else
                {
                    MessageBox.Show(Resources.ADDALGDescr_InvalidExeExtText, 
                        Resources.ADDALGDescr_InvalidLibraryExtTitle, MessageBoxButtons.OK);
                }
            }
        }*/
    }
}
